/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package catchtheeggs;

import basket.basket;
import fallOutObjects.fallOutObjects;
import fallOutObjects.fallOutObjects.kindOfObject;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import poultry.chicken;
import poultry.duck;
import poultry.geese;
import poultry.poultry;

/**
 *
 * @author Windows7
 */
public class theBoard extends JPanel implements Runnable {

    private basket myBasket;
    private ArrayList<poultry> allPoultry;
    
    private int brokenEggs;
    private int shits;
    
    private int B_WIDTH;
    private int B_HEIGHT;
    
    private boolean ingame;
    
    private int Eggs;
    
    private boolean makeChange = true;
    private int speed;
    
    protected Graphics2D bufferGraphic;
    
    protected Image buffer;
    
    public theBoard()
    {
        this.brokenEggs = 0;
        this.shits = 0;
        this.allPoultry = new ArrayList<>(); 
        this.addKeyListener(new TAdapter());
        this.setFocusable(true);
        this.setDoubleBuffered(true);
        this.myBasket = new basket(800);
        this.ingame = true;
        this.setSize(800, 600);
        this.Eggs = 0;
        
        this.buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_ARGB);
        this.bufferGraphic = (Graphics2D) this.buffer.getGraphics();
        this.setOpaque(false);

    }
    
    @Override
    public void addNotify()
    {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();
    }

    
    
    @Override
    public void run() {
        //int test = 0;
        while(ingame)
        {
            this.repaint();
            if(this.brokenEggs >= 3 || this.shits >= 3)
            {
                this.ingame = false;
                this.repaint();
            }
            else
            {
                
                int n = this.allPoultry.size();
                
                for(int i = 0 ; i < n ; i++)
                {
                    try {
                        if(Eggs % 20 == 0)
                        {
                            if(makeChange == true)
                            {
                                this.speed ++;
                                this.makeChange = false;
                            }
                        }
                        else
                        {
                            this.makeChange = true;
                        }
                        boolean result = this.allPoultry.get(i).move(this.speed);
                        if(result == true)
                        {
                            //test ++;
                            //System.out.println("brokenEggs " +test);
                            this.brokenEggs = this.brokenEggs + 1;
                        }

                        
                    } catch (IOException ex) {
                        Logger.getLogger(theBoard.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                //this.repaint();
                this.myBasket.move();
                this.checkCollisions();
                
                this.repaint();
                try {
                    Thread.currentThread().sleep(5);
                } catch (InterruptedException ex) {
                    Logger.getLogger(theBoard.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    @Override
    public void paint(Graphics g)
    {
        if(this.ingame)
        {
            
            this.reLoad();
            drawAllPoultry();
            drawYourScore();
            drawBasket();
            Graphics2D g2d = (Graphics2D) g;
            this.buffer.flush();
            g2d.drawImage(this.buffer,0,0,this);
            //this.reLoad();
            
        }
        else
        {
            Graphics2D g2d = (Graphics2D) g;
            String msg = "Game Over";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g2d.setColor(Color.black);
            g2d.setFont(small);
            g2d.drawString(msg, (B_WIDTH - metr.stringWidth(msg)) / 2, B_HEIGHT / 2);
        }
        
        Toolkit.getDefaultToolkit().sync();
        //g.dispose();
    }
    
    public void reLoad()
    {
        this.buffer = new BufferedImage(800,600,BufferedImage.TYPE_INT_ARGB);
        this.bufferGraphic = (Graphics2D) this.buffer.getGraphics();
        this.bufferGraphic.setColor(Color.white);
        this.bufferGraphic.fillRect(0, 0, 800, 600);

    }
    
    public synchronized void drawYourScore()
    {
         this.bufferGraphic.setColor(Color.BLACK);
         this.bufferGraphic.drawString("Your shits: " + this.shits + "/3", 5, 15);
         this.bufferGraphic.drawString("Your broken eggs: " + this.brokenEggs+ "/3", 5, 30);
         this.bufferGraphic.drawString("Eggs in your basket: " + this.Eggs, 5, 60);
         this.bufferGraphic.drawString("speed: " + this.speed, 5, 75);
    }
    
    public synchronized void drawBasket()
    {
        this.myBasket.draws(this.bufferGraphic);
    }
    
    public synchronized void drawAllPoultry()
    {
        int n = this.allPoultry.size();
        for(int i = 0 ; i < n ; i++)
        {
            this.allPoultry.get(i).draws(this.bufferGraphic);
        }
    }
    
        
    public void checkCollisions()
    {
        Rectangle r3 = this.myBasket.getBounds();
        int n = this.allPoultry.size();
        
        for(int i = 0 ; i < n; i++)
        {
            poultry temp = this.allPoultry.get(i);
            ArrayList<fallOutObjects> e = temp.getObjectsFallOut();
            int m = temp.getObjectsFallOut().size();
            for(int j = 0; j < m ; j ++)
            {
                fallOutObjects b = e.get(j);
                Rectangle r2 = b.getBounds();
                if (r3.intersects(r2)) 
                {
                    b.setVisible(false);
                    if(b.getName() == kindOfObject.egg)
                    {
                        this.Eggs ++;
                    }
                    else if(b.getName() == kindOfObject.shit)
                    {
                        this.shits ++;
                    }
                }
                b = null;
                r2 = null;
            }
            temp.deleteAllCraps();
            temp = null;
            e = null;
        }
        r3 = null;
    }
    
    //public void paint All
    
    private class TAdapter extends KeyAdapter 
    {

        @Override
        public void keyReleased(KeyEvent e) {
            
            int key = e.getKeyCode();
            if(key != KeyEvent.VK_G && key != KeyEvent.VK_N && key != KeyEvent.VK_V)
            {
                myBasket.keyReleased(e);
            }
            else
            {
                
            }
        }

        @Override
        public void keyPressed(KeyEvent e) {
            try {
                int key = e.getKeyCode();
                if(key != KeyEvent.VK_G && key != KeyEvent.VK_N && key != KeyEvent.VK_V)
                {
                    myBasket.keyPressed(e);
                }
                else
                {
                    if(key == KeyEvent.VK_G)
                    {
                        Random generator = new Random(); 
                        int x = generator.nextInt(700) + 1;
                        allPoultry.add(new chicken(x, 0));
                    }
                    else if(key == KeyEvent.VK_N)
                    {
                        Random generator = new Random(); 
                        int x = generator.nextInt(700) + 1;
                        allPoultry.add(new geese(x, 0));
                    }
                    else if(key == KeyEvent.VK_V)
                    {
                        Random generator = new Random(); 
                        int x = generator.nextInt(700) + 1;
                        allPoultry.add(new duck(x, 0));
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(theBoard.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
    
}
